

package ru.samgtu.wst.ui.swing;

import ru.samgtu.wst.model.ConversationID;
import ru.samgtu.wst.model.ConversationModel;
import javax.swing.JFrame;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;


public class ShowConversationAction extends AbstractAction {

	private ConversationModel _model;

	/** Creates a new instance of ShowConversationAction */
	public ShowConversationAction(ConversationModel model) {
		_model = model;
		putValue(NAME, "Show conversation");
		putValue(SHORT_DESCRIPTION,
				"Opens a new window showing the request and response");
		putValue("CONVERSATION", null);
	}

	public void actionPerformed(ActionEvent e) {
		Object o = getValue("CONVERSATION");
		if ((o == null) || !(o instanceof ConversationID)) {
			return;
		}
		ConversationID id = (ConversationID) o;
		ConversationPanel cp = new ConversationPanel(_model);
		cp.setSelectedConversation(id);
		JFrame frame = cp.inFrame();
		frame.setVisible(true);
		frame.toFront();
		frame.requestFocus();
	}

	@Override
	public void putValue(String key, Object value) {
		super.putValue(key, value);
		if ((key != null) && key.equals("CONVERSATION")) {
			if ((value != null) && (value instanceof ConversationID)) {
				setEnabled(true);
			} else {
				setEnabled(false);
			}
		}
	}

}
